<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户资料</title>

    <link rel="stylesheet" href="./assets/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="./assets/css/animate.min.css"/>
    <link rel="stylesheet" href="./assets/bootstrap-validator/bootstrapValidator.min.css">
    <link rel="stylesheet" href="./assets/mui/css/mui.min.css">
    <link rel="stylesheet" href="./assets/city-picker/city-picker.css">

    <link rel="stylesheet/less" href="./assets/css/reset.less"/>
    <link rel="stylesheet/less" href="./assets/css/common.less"/>
    <link rel="stylesheet/less" href="./assets/css/UserProfile.less"/>
</head>
<body>
<!-- 用户信息 -->
<div class="user">
    <div class="avatar wow fadeInUp">
        <img src="./assets/images/avatar.png"/>
    </div>
</div>

<form class="profile wow fadeInUp" data-wow-delay="200ms" id="profile">
    <div class="form-group">
        <label for="nickname">姓名</label>
        <input type="text" class="form-control" id="nickname" placeholder="请输入姓名" required/>
    </div>
    <div class="form-group">
        <label for="mobile">手机号</label>
        <input type="text" class="form-control" id="mobile" placeholder="请输入手机号" readonly/>
    </div>


    <div class="form-group">
        <label for="email">邮箱</label>
        <input type="email" class="form-control" id="email" placeholder="请输入邮箱" required/>
    </div>

    <div class="form-group">
        <label for="password">密码</label>
        <input type="password" class="form-control" id="password" placeholder="请输入密码" required/>
    </div>

    <div class="form-group">
        <label for="gender">性别</label>
        <select class="form-control" id="gender">
            <option>请选择</option>
            <option value="0">保密</option>
            <option value="1">男</option>
            <option value="2">女</option>
        </select>
    </div>

    <div class="form-group" style="position: relative;">
        <label for="region">地区</label>
        <input type="text" class="form-control" id="region" placeholder="请选择地区" readonly data-toggle="city-picker"
               data-responsive="true" style="width:100%;"/>
    </div>

    <div class="form-group">
        <label for="address">详细地址</label>
        <input type="text" class="form-control" id="address" placeholder="请输入详细地址" required/>
    </div>

    <input type="file" id="avatar" name="avatar">

    <div class="action">
        <button class="btn">提交</button>
    </div>
</form>
</body>
<script src="./assets/js/less.min.js"></script>
<script src="./assets/js/jquery.min.js"></script>
<script src="./assets/js/bootstrap.min.js"></script>
<script src="./assets/js/wow.min.js"></script>
<script src="./assets/bootstrap-validator/bootstrapValidator.min.js"></script>
<script src="./assets/mui/js/mui.min.js"></script>
<script src="./assets/axios/axios.min.js"></script>
<script src="./assets/axios/request.js"></script>
<script src="./assets/city-picker/city-picker.data.min.js"></script>
<script src="./assets/city-picker/city-picker.min.js"></script>
<script src="./assets/js/cookie.js"></script>
<script src="./assets/js/login.js"></script>
<script>
    new WOW().init()

    let business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {}

    if (JSON.stringify(business) !== '{}') {
        $('#mobile').val(business.mobile)
        $('#nickname').val(business.nickname)
        $('#gender').val(business.gender)
        $('#address').val(business.address)
        $('.avatar img').attr('src', business.avatar)
    }

    $('#profile').bootstrapValidator({
        message: '表单内容默认不能为空',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            nickname: {
                validators: {
                    notEmpty: {
                        message: '请输入姓名'
                    }
                }
            }
        }
    }).on('success.form.bv', (e) => {
        e.preventDefault()
    }).on('submit', async () => {
        if (!$('#profile').data('bootstrapValidator').isValid()) {
            $('#profile').data('bootstrapValidator').resetForm()
            return
        }

        let data = {
            id: business.id,
            mobile: business.mobile,
            nickname: $.trim($('#nickname').val()),
            gender: $.trim($('#gender').val()),
            address: $.trim($('#address').val())
        }

        let password = $.trim($('#password').val())

        if (password) {
            data.password = password;
        }

        let avatar = $('#avatar')[0].files[0]

        if (avatar) {
            data.avatar = avatar
        }

        let cityPicker = $('#region').data('citypicker')
        let code = cityPicker.getCode('district') || cityPicker.getCode('city') || cityPicker.getCode('province')

        if (code) {
            data.code = code
        }

        let result = await UPLOAD({
            url: 'business/profile',
            params: data
        })

        if (result) {
            if (result.code === 1) {
                mui.toast(result.msg)
                cookie.set('business', JSON.stringify(result.data))
                setTimeout(() => {
                    history.go(-1)
                }, 2000)
            } else {
                mui.toast(result.msg)
            }
        }
    })

    $('.avatar').click(function () {
        $('#avatar').click()
    })

    $('#avatar').change(function () {
        let file = $(this)[0].files[0]

        if (file) {
            let reader = new FileReader()
            reader.readAsDataURL(file)
            reader.onload = function () {
                $('.avatar img').attr('src',reader.result)
            }
        }
    })
</script>
</html>